###### Makefile ######

######################################
# target
######################################
TARGET := sgl.a
SGL_CONFIG_DIR := $(SGL_CONF)

######################################
# building variables
######################################
# debug build?
DEBUG = 0
# optimization
OPT = -O2#-flto

######################################
# source
######################################
SGL_DIR = ../source
# C sources
C_SOURCES =  \
$(wildcard $(SGL_DIR)/*.c)  \
$(wildcard $(SGL_DIR)/core/*.c)  \
$(wildcard $(SGL_DIR)/libs/*.c)  \
$(wildcard $(SGL_DIR)/libs/heap/*.c)  \
$(wildcard $(SGL_DIR)/draw/*.c)  \
$(wildcard $(SGL_DIR)/fonts/*.c)  \
$(wildcard $(SGL_DIR)/widget/*.c)  \


# C defines
C_DEFS = $(PLATFORM_ARGS)
# AS includes
AS_INCLUDES = 

# C includes
C_INCLUDES =     \
-I$(SGL_CONFIG_DIR) \
-I$(SGL_DIR)    \
-I$(SGL_DIR)/core    \
-I$(SGL_DIR)/libs    \
-I$(SGL_DIR)/libs/heap    \
-I$(SGL_DIR)/draw  \
-I$(SGL_DIR)/font   \
-I$(SGL_DIR)/widget   \


BUILD_DIR    := obj
#######################################
# Compiler Setting
#######################################


CFLAGS := -Wall -std=$(STD_FLAG)  $(C_INCLUDES) $(C_DEFS)

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2 
endif

# default action: build all
all: $(TARGET)

#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))


$(BUILD_DIR)/%.o: %.c sgl.mk | $(BUILD_DIR)
	@echo "CC $<"
	@$(CC) -c $(CFLAGS) -MMD -MP \
		-MF  $(BUILD_DIR)/$(notdir $(<:.c=.d)) \
		-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(TARGET): $(OBJECTS) sgl.mk
	@echo "AR $@"
	@$(AR) rcs $@ $(OBJECTS)

$(BUILD_DIR):
	mkdir $@

#######################################
# clean up
#######################################
.PHONY: clean

clean:
	rm -fR $(BUILD_DIR) $(TARGET)
